from profile.models import *
from profile.forms import *
from django.template import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth import authenticate, login as do_login, logout as do_logout

def activate(request, key):
    profile = Profile.objects.activate(key)
    if profile:
        return HttpResponseRedirect(reverse('auth_login'))
    return HttpResponse('You must register') 

def index(request):
    rc = RequestContext(request)
    return render_to_response('profile/index.html', rc)

def list(request):
    profiles = Profile.objects.all().filter(activation_key = "Activated")
    for profile in profiles:
        print profile
    rc = RequestContext(request, {'profile_list' : profiles})
    return render_to_response('profile/profile_list.html', rc)

def show(request, id):
    user = get_object_or_404(User, pk = id)
    profile = get_object_or_404(Profile, user = user)
    rc = RequestContext(request, {'profile' : profile})
    return render_to_response('profile/profile.html', rc)

def edit(request):
    user = get_object_or_404(User, pk = request.user.id)
    profile = get_object_or_404(Profile, user = user)
    profile.activated = True
    if request.method == "POST":
        pf = ProfileForm(request.POST, instance = profile)
        if pf.is_valid():
            pf.save()
            return HttpResponseRedirect(reverse('auth_profile_show', args=(user.id,)))
    else:
        pf = ProfileForm(instance = profile)
    rc = RequestContext(request, {'form' : pf})
    return render_to_response('profile/profile_edit.html', rc)

def register(request):
    if request.method == "POST":
        rf = RegistrationForm(request.POST)
        if rf.is_valid():
            rf.save()
            return HttpResponse('You must validate your registration. Instruction was sent to you email.')
    else:
        rf = RegistrationForm()
    rc = RequestContext(request, {'form' : rf})
    return render_to_response('profile/register_form.html', rc)
    
def login(request):
    if request.method == "POST":
        lf = LoginForm(request.POST)
        if lf.is_valid():
            user = authenticate(username = lf.cleaned_data.get("username"), password = lf.cleaned_data.get("password"))
            if user:
                do_login(request, user)
                profile = get_object_or_404(Profile, user = user)
                if not profile.activated:
                    return HttpResponseRedirect(reverse('auth_profile_edit'))
                next = request.POST.get('next', 'auth_index')
                return HttpResponseRedirect(reverse(next))
            else:
                return HttpResponseRedirect(reverse('auth_login'))
    else:
        lf = LoginForm()
    rc = RequestContext(request, {'form' : lf})
    return render_to_response('profile/login.html', rc)
    
def logout(request):
    do_logout(request)
    return HttpResponseRedirect(reverse('auth_index'))

def password_change_done(request):
    return render_to_response('profile/password_change_done.html')

def avatar(request):
    if request.method == "POST":
        form = AvatarLoad(request.POST)
        if form.is_valid():
            return HttpResponse("Well done")
    else:
        form = AvatarLoad()
    rc = RequestContext(request, {'form' : form})
    return render_to_response('profile/avatar.html', rc)
